package HelloServiceDo

import (
	"bytes"
	"context"
	"encoding/json"
	"goGrpc/helloService/DataBaseUtils"
	"goGrpc/helloService/Utils"
	"log"
)

func MyEsTest() {
	var titleInfo = "测试es"
	var buf bytes.Buffer
	query := map[string]interface{}{
		"query": map[string]interface{}{
			"match_phrase": map[string]interface{}{
				"title": titleInfo,
			},
		},
	}
	if err := json.NewEncoder(&buf).Encode(query); err != nil {
		HelloServiceUtils.FailOnError(err, "Error encoding query")
	}
	log.Printf("查询的入参为，buf=%+v	\n", &buf)
	var es = DataBaseUtils.EsConnection
	// Perform the search request.
	res, err := es.Search(
		es.Search.WithContext(context.Background()),
		es.Search.WithIndex("grpchellotest"),
		es.Search.WithBody(&buf),
		es.Search.WithTrackTotalHits(true),
		es.Search.WithPretty(),
	)
	if err != nil {
		HelloServiceUtils.FailOnError(err, "Error getting response")
	}
	defer res.Body.Close()
	log.Println(res.String(), "es查询结果")
}
